package com.quanyue.music.file.controller;

import com.quanyue.auto.email.domain.AutoSendEmail;
import com.quanyue.auto.email.domain.EmailConfig;
import com.quanyue.auto.email.service.EmailConfigService;
import com.quanyue.auto.email.service.EmailService;
import com.quanyue.auto.util.ServiceResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Api(tags = "邮件")
@RestController
@RequestMapping("/api")
public class EmailContoller {

    @Autowired
    private EmailService emailService;

    @Autowired
    private EmailConfigService emailConfigService;

    @ApiOperation(value = "添加邮件接收方")
    @PostMapping(value = "/email/add", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<Boolean> add(@RequestBody AutoSendEmail email) {
        emailService.add(email);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "修改单个邮件接收方信息")
    @PutMapping(value = "/email/edit", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<Boolean> edit(@RequestBody AutoSendEmail email) {
        emailService.edit(email);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "接收方邮件列表")
    @GetMapping(value = "/email/list", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<List<AutoSendEmail>> list() {
        return ServiceResult.success(emailService.emailList());
    }

    @ApiOperation(value = "删除单个邮件接收方")
    @DeleteMapping(value = "/email/delete", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<Boolean> delete(@Param("id") String id) {
        emailService.delete(id);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "邮件配置")
    @GetMapping(value = "/email/config", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<EmailConfig> getEmailConfig() {
        return ServiceResult.success(emailConfigService.getEmailConfig());
    }

    @ApiOperation(value = "添加配置")
    @PostMapping(value = "/email/config/add", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<Boolean> addEmailConfig(@RequestBody EmailConfig emailConfig) {
        emailConfigService.addEmailConfig(emailConfig);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "修改配置")
    @PutMapping(value = "/email/config/edit", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<Boolean> editEmailConfig(@RequestBody EmailConfig emailConfig) {
        emailConfigService.editEmailConfig(emailConfig);
        return ServiceResult.SUCCESS;
    }
}
